package demoMod.invincibleOne.cards.invincible;

import basemod.helpers.CardModifierManager;
import com.badlogic.gdx.math.Vector2;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.SpawnMonsterAction;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.monsters.exordium.AcidSlime_M;
import com.megacrit.cardcrawl.monsters.exordium.AcidSlime_S;
import com.megacrit.cardcrawl.monsters.exordium.SpikeSlime_M;
import com.megacrit.cardcrawl.monsters.exordium.SpikeSlime_S;
import com.megacrit.cardcrawl.rooms.MonsterRoomBoss;
import com.megacrit.cardcrawl.rooms.MonsterRoomElite;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.interfaces.MonsterDeathHook;
import demoMod.invincibleOne.modifiers.ExhaustAtStartOfCombatMod;

public class CrowdTactics extends AbstractInvincibleCard implements MonsterDeathHook {
    public static final String ID = InvincibleOneMod.makeID("CrowdTactics");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/CrowdTactics.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.RARE;
    private static final CardTarget TARGET = CardTarget.SELF;

    private static final int COST = -2;

    private boolean triggered = false;

    public CrowdTactics() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.modifier = new ExhaustAtStartOfCombatMod();
        CardModifierManager.addModifier(this, modifier);
        this.tags.add(CardTagsEnum.ALERT);
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> {
            this.rawDescription = cardStrings.UPGRADE_DESCRIPTION;
            this.initializeDescription();
        };
    }

    @Override
    public boolean onBattleStart() {
        triggered = false;
        return true;
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {

    }

    @Override
    public void onMonsterDeath(AbstractMonster m) {
        if (!triggered && !AbstractDungeon.getMonsters().areMonstersBasicallyDead() &&
                !(AbstractDungeon.getCurrRoom() instanceof MonsterRoomBoss)) {
            int spawnAmount = (int) AbstractDungeon.getMonsters().monsters.stream().filter(monster -> !monster.isDeadOrEscaped()).count();
            Vector2 vector2 = new Vector2(120.0F * Settings.scale, 0.0F);
            for (int i=0;i<spawnAmount;i++) {
                AbstractMonster slime;
                if (upgraded) {
                    if (AbstractDungeon.cardRandomRng.randomBoolean()) {
                        slime = new AcidSlime_M(0.0F, 0.0F);
                    } else {
                        slime = new SpikeSlime_M(0.0F, 0.0F);
                    }
                } else {
                    if (AbstractDungeon.cardRandomRng.randomBoolean()) {
                        slime = new AcidSlime_S(0.0F, 0.0F, 0);
                    } else {
                        slime = new SpikeSlime_S(0.0F, 0.0F, 0);
                    }
                }
                slime.drawX = m.drawX - vector2.x;
                slime.drawY = m.drawY - vector2.y;
                addToBot(new SpawnMonsterAction(slime, true));
                addToBot(new AbstractGameAction() {
                    @Override
                    public void update() {
                        slime.createIntent();
                        isDone = true;
                    }
                });
                vector2.rotate(45.0F);
            }
            triggered = true;
        }
    }
}
